﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;
using System.Text.RegularExpressions;
using System.ComponentModel;

namespace TVGrid.ViewModel
{
    public class NewStationViewModel : ViewModelBase, IDataErrorInfo
    {
        private string _name;
        public string Name
        {
            get { return _name; }
            set
            {
                _name = value;
                OnPropertyChanged("Name");
            }
        }

        private string _url;
        public string URL
        {
            get { return _url; }
            set
            {
                _url = value;
                OnPropertyChanged("Url");
            }
        }

        private string _errorMag;
        public string ErrorMag
        {
            get { return _errorMag; }
            set
            {
                _errorMag = value;
                OnPropertyChanged("ErrorMag");
            }
        }

        private string _errorMagTt;
        public string ErrorMagTT
        {
            get { return _errorMagTt; }
            set
            {
                _errorMagTt = value;
                OnPropertyChanged("ErrorMagTT");
            }
        }

        private bool _isExpanded;
        public bool IsExpanded
        {
            get { return _isExpanded; }
            set { _isExpanded = value; }
        }


        public NewStationViewModel()
        {
            _isExpanded = false;
        }

        #region AddStationCommand
        RelayCommand _addStationCommand;
        public ICommand AddStationCommand
        {
            get
            {
                if (_addStationCommand == null)
                {
                    _addStationCommand = new RelayCommand(p => this.AddStation(p),
                    p => this.CanAddStationCommand(p));
                }
                return _addStationCommand;
            }
        }

        private bool CanAddStationCommand(object p)
        {
            Regex reg = new Regex(@"\b(((\S+)?)(@|mailto\:|(news|(ht|f)tp(s?))\://)\S+)\b");
            return (Name != null && URL != null && reg.IsMatch(URL));
        }

        private object AddStation(object p)
        {
            try
            {
                ServiceClient.AddStationAsync(Name, URL);
            }
            catch(Exception ex)
            {
                ErrorMag = "Failed to add station";
                ErrorMagTT = ex.Message;
            }
            Name = null;
            URL = null;
            _isExpanded = false;
            StationAdded();
            return null;
        }
        #endregion

        

        public string Error
        {
            get { throw new NotImplementedException(); }
        }

        public string this[string columnName]
        {
            get 
            {

                string result = null;

                if (columnName == "Name")
                    if (string.IsNullOrEmpty(Name))
                        result = "Please enter station name";

                if (columnName == "URL")
                    if (string.IsNullOrEmpty(URL))
                        result = "Please enter station URL";

                return result;
            }
        }
    }
}
